package com.hrt.demo.czd.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.demo.czd.bean.Address;
import com.hrt.demo.czd.bean.User;
import com.hrt.demo.czd.service.AddressService;
import com.hrt.demo.czd.service.UserService;
@Controller
@RequestMapping(value="manage/demo/czd/user")
public class UserController extends BaseController<User> {
	@Autowired
	private UserService userService;
	@Autowired
	private AddressService addressService;
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(
			HttpServletRequest request, HttpServletResponse response) {
		User user = new User();
		String email = request.getParameter("email");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String mobile = request.getParameter("mobile");
		String address = request.getParameter("address");
		Address ads = new Address();
		ads.setName(address);
		
		user.setEmail(email);
		user.setUsername(username);
		user.setPassword(password);
		user.setMobile(mobile);
		List<Address> adds = new ArrayList<Address>();
		adds.add(ads);
		user.setAddress(adds);
		ads.setUser_enti(user);
		ModelAndView model = new ModelAndView();
		System.out.println("save");
			try {
				userService.saveUser(user);
				addressService.saveAddress(ads);

				response.sendRedirect("list.do");
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		
	}
	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		System.out.println("list");
		list = userService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(userService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		view.addObject("pageResult", page);
		return view;

	}
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("update");
		User user = new User();
		Long userId = Long.parseLong(request.getParameter("id"));
		System.out.println("userId::"+userId);
		String email = request.getParameter("email");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String mobile = request.getParameter("mobile");
		String address = request.getParameter("address");
		Address ads = new Address();
		ads.setName(address);
		
		user.setId(userId);
		user.setEmail(email);
		user.setUsername(username);
		user.setPassword(password);
		user.setMobile(mobile);
		List<Address> adds = new ArrayList<Address>();
		adds.add(ads);
		user.setAddress(adds);
		ads.setUser_enti(user);
		ModelAndView model = new ModelAndView();
		
			try {
				addressService.updateAddress(ads);
				userService.updateUser(user);
				
				response.sendRedirect("list.do");
			} catch (IOException e) {
				e.printStackTrace();
			}
		
	}
	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) String userId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (userId != null && !"".equals(userId)) {
			System.out.println("showshowshowshowshow " + userId);
			User user = this.userService
					.getUserByid(new Long(userId));
			if (user != null) {
				map = transBean2Map(user);
			}

		}
		return map;

	}
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		try {
			
			userService.deleteById(idArr);
				
			return idArr;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
	/**
	 * 
	 * @param pageIndex
	 * 
	 * @param pageSize
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(userService.totalCount());

		List<User> resultList = userService.queryByPage(
				getPageIndex(), getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}
}
